package com.ruoyi.business.controller;

import com.ruoyi.business.domain.VisitorRecords;
import com.ruoyi.business.param.VisitorCountXmlParam;
import com.ruoyi.business.service.IVisitorRecordsService;
import com.ruoyi.business.vo.VisitorRecordsExportVo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 访客记录Controller
 * 
 * @author qiancheng
 * @date 2025-06-13
 */
@Api(tags = "访客记录管理")
@RestController
@RequestMapping("/business/records")
public class VisitorRecordsController extends BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(VisitorRecordsController.class);
    
    @Autowired
    private IVisitorRecordsService visitorRecordsService;

    /**
     * 首页->>访问数据->>查询访客记录列表
     */
    @ApiOperation("查询访客记录列表")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam("访客记录查询条件") VisitorRecords visitorRecords)
    {
        startPage();
        List<VisitorRecords> list = visitorRecordsService.selectVisitorRecordsList(visitorRecords);
        return getDataTable(list);
    }

    /**
     * 导出访客记录列表
     */
    @ApiOperation("导出访客记录列表")
    @Anonymous
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ApiParam("访客记录查询条件") VisitorRecords visitorRecords)
    {
        List<VisitorRecords> list = visitorRecordsService.selectVisitorRecordsList(visitorRecords);
        ExcelUtil<VisitorRecords> util = new ExcelUtil<VisitorRecords>(VisitorRecords.class);
        util.exportExcel(response, list, "访客记录数据");
    }

    /**
     * 获取访客记录详细信息
     */
    @ApiOperation("获取访客记录详细信息")
    @PreAuthorize("@ss.hasPermi('business:records:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam("访客记录ID") @PathVariable("id") Long id)
    {
        return success(visitorRecordsService.selectVisitorRecordsById(id));
    }

    /**
     * 首页->访问数据->>新增访客记录
     */
    @ApiOperation("新增访客记录")
    @Anonymous
    @PostMapping
    public AjaxResult add(@ApiParam("访客记录信息") @RequestBody VisitorRecords visitorRecords)
    {
        try {
            // 输入数据验证
            if (visitorRecords.getVisitUnit() == null || visitorRecords.getVisitUnit().trim().isEmpty()) {
                return error("访问单位不能为空");
            }
            if (visitorRecords.getVisitCount() == null || visitorRecords.getVisitCount() <= 0) {
                return error("访问人数必须为正整数");
            }
            if (visitorRecords.getVisitTime() == null) {
                return error("访问时间不能为空");
            }
            return toAjax(visitorRecordsService.insertVisitorRecords(visitorRecords));
        } catch (Exception e) {
            logger.error("新增访客记录失败", e);
            return error("新增访客记录失败：" + e.getMessage());
        }
    }

    /**
     * 首页->>访问数据->>修改访客记录
     */
    @ApiOperation("修改访客记录")
    @Anonymous
    @PutMapping
    public AjaxResult edit(@ApiParam("访客记录信息") @RequestBody VisitorRecords visitorRecords)
    {
        try {
            // 输入数据验证
            if (visitorRecords.getId() == null) {
                return error("记录ID不能为空");
            }
            if (visitorRecords.getVisitUnit() != null && visitorRecords.getVisitUnit().trim().isEmpty()) {
                return error("访问单位不能为空字符串");
            }
            if (visitorRecords.getVisitCount() != null && visitorRecords.getVisitCount() <= 0) {
                return error("访问人数必须为正整数");
            }
            return toAjax(visitorRecordsService.updateVisitorRecords(visitorRecords));
        } catch (Exception e) {
            logger.error("修改访客记录失败", e);
            return error("修改访客记录失败：" + e.getMessage());
        }
    }

    /**
     * 首页->>访问数据->>删除访客记录
     */
    @ApiOperation("删除访客记录")
    @Anonymous
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("访客记录ID数组") @PathVariable Long[] ids)
    {
        return toAjax(visitorRecordsService.deleteVisitorRecordsByIds(ids));
    }

    /**
     * 首页->>访问数据->>获取访客数据统计信息
     * @return 访客数据视图对象
     */
    @ApiOperation("获取访客数据统计信息")
    @Anonymous
    @GetMapping("/getStatistics")
    public AjaxResult getVisitStatistics(@ApiParam("访客记录查询条件") VisitorRecords visitorRecords) {
        try {
            return success(visitorRecordsService.getVisitStatistics(visitorRecords));
        } catch (Exception e) {
            logger.error("获取访客数据统计信息失败", e);
            return error("获取访客数据统计信息失败：" + e.getMessage());
        }
    }


    /**
     * 首页->>访问数据->>更多->>根据年月周获取每天访问人数
     */
    @ApiOperation("根据年月周获取每天访问人数")
    @Anonymous
    @GetMapping("/getCountByDate")
    public AjaxResult getCountByDate(@ApiParam("访客统计查询条件") VisitorCountXmlParam VCountXmlParam)
    {
        return success(visitorRecordsService.getCountByDate(VCountXmlParam));
    }

    /**
     * 首页->>访问数据->>更多->>根据年月周获取每天访问列表
     */
    @ApiOperation("根据年月周获取每天访问列表")
    @Anonymous
    @GetMapping("/getListByDate")
    public TableDataInfo getListByDate(@ApiParam("访客统计查询条件") VisitorCountXmlParam VCountXmlParam)
    {
        startPage();
        List<VisitorRecords> list = visitorRecordsService.getListByDate(VCountXmlParam);
        return getDataTable(list);
    }

    /**
     * 根据年月周导出访客记录列表
     * @param response
     * @param VCountXmlParam
     */
    @ApiOperation("根据年月周导出访客记录列表")
    @Anonymous
    @PostMapping("/getListByDateExport")
    public void getListByDateExport(HttpServletResponse response, @ApiParam("访客记录查询条件") VisitorCountXmlParam VCountXmlParam)
    {
        List<VisitorRecordsExportVo> listByDateExport = visitorRecordsService.getListByDateExport(VCountXmlParam);
        ExcelUtil<VisitorRecordsExportVo> util = new ExcelUtil<VisitorRecordsExportVo>(VisitorRecordsExportVo.class);
        util.exportExcel(response, listByDateExport, "访客记录数据");
    }
}
